clear
use user_quarter_choice_cholesterol


// focus on cholesterol
tab indic_id
keep if indic_id == 5


// BASIC: LOOK AT IMPACT OF FIRST CHOICE ON LATER CHOICE 
// generate different subsequent outcome variables
egen first_q = min(quarter), by(enrolid)
gen first_choice_temp = generic_id if quarter == first_q
egen first_choice = max(first_choice_temp), by(enrolid)

gen first_choice_drugid_temp = drugid if quarter == first_q
egen first_choice_drugid = max(first_choice_drugid_temp), by(enrolid)

// merge on the gennme
merge m:1 generic_id using generic_name_mapping
keep if _merge == 3
drop _merge

gen first_gennme = gennme if quarter == first_q
sort enrolid quarter
replace first_gennme = first_gennme[_n-1] if enrolid == enrolid[_n-1]


// generate outcome variables
gen period = quarter - first_q
gen chose_same_compound = (first_choice == generic_id)
gen chose_same_option = (first_choice_drugid == drugid)
gen overlap = strpos(gennme, first_gennme) > 0 | strpos(first_gennme, gennme) > 0


gen first_generic = generic if quarter == first_q
sort enrolid quarter
replace first_generic = first_generic[_n-1] if enrolid == enrolid[_n-1]




set scheme s1mono
graph set window fontface "Palatino Linotype"

collapse (mean) chose_same_option chose_same_compound overlap, by(period)

replace period = period / 4

twoway (line chose_same_option period) (line chose_same_compound period, lpattern(dash_dot)) ///
(line overlap period, lpattern(longdash)), legend(lab(1 "Same Option") lab(2 "Same Compound") lab(3 "Molecule Overlap") size(large)) ///
xtitle("Years Since First Prescription", size(large)) ///
ytitle("User Fraction", size(large)) ///
xlabel(,labsize(large)) ylabel(,labsize(large))


graph save reduced_form_stickiness, replace
graph export reduced_form_stickiness.pdf, replace 



